
expdir=$1
stage=2
num_gpus=4

asr_zh=(
#"test_20241119_aishell2_mic"
#"test_20241119_aishell2_android"
#"test_20241119_aishell2_ios"
"test_20221110_tlmeetwebinar_10h"
"test_20211229_wenetspeech_meeting_org_fbankhires"
)
asr_zhen=(
"test_20220221_ailab_5h_org_fbankhires"
"test_20220509_tal_5h_org_fbankhires"
)

asr_en=(
"test_20220407_librispeech_clean_org_fbankhires"
"test_20220407_librispeech_other_org_fbankhires"
)

asr_draft=(
"valid_20240322_cn_slides_1100h"
"test_20241119_slidespeech"
)
asr_multi=(
"valid_20240322_cn_slides_1100h_asr_multi"
)
asr_test=(
"test_aishell1"
"dev_aishell1"
)
asr_yb=(
"context_test/yuanbao_pst_test_long"
"context_test/yuanbao_pst_sf"
"context_test/yuanbao_pst_test_long_300_kw_wemeet"
"context_test/yuanbao_pst_test_long_exact_kw_wemeet"
"context_test/yuanbao_pst_test_long_exact_kw_hunyuan"
)
combined_list=(
${asr_yb[@]}
${asr_draft[@]}
${asr_zh[@]}
${asr_zhen[@]}
#${asr_multi[@]}
${asr_en[@]}
)
diar_list=(
${asr_multi[@]}
)
decoding_name=tests

orig_dir=/apdcephfs_qy3/share_976139/users/adrenzhou/audio_llm/raw_data/processed
#mkdir -p $expdir/tests_normal_prompt
if [ $stage -le 0 ]; then

	echo "1. Evaluating ASR"
	#for dataset in ${combined_list[@]}; do
	for dataset in ${asr_en[@]}; do
	#for dataset in valid_20250324_cn_slides; do
	# for dataset in valid_20240322_cn_slides_1100h context_test/yuanbao_pst_test_long; do
	#for dataset in ${asr_yb[@]}; do
		out_dir=$expdir/$decoding_name/$dataset
		mkdir -p $out_dir
		echo "Start Decoding Time: ", `date` > $out_dir/timelines.txt
		python batch_infer_moeconformer_llm.py --input_data $orig_dir/${dataset}.jsonl --output_data $out_dir/result.jsonl --model_path $expdir --batch_size 1 --num_gpus $num_gpus

		echo "End Decoding Time: ", `date` >> $out_dir/timelines.txt
		python tools/collect_and_wer.py $out_dir/result.jsonl $out_dir/text.ref $out_dir/text.hyp $out_dir/wer &
		echo $out_dir/wer
	done
fi
wait

# exit 0

if [ $stage -le 2 ]; then
      echo "Computing ITN Results"
	names=("yuanbao_pst_test_long" "yuanbao_pst_sf")
	paths=(
		"/apdcephfs_qy3/share_976139/data/asr/test/zh/test_20250228_yuanbao_pst1/pst_long_20250224_to_20250228/ref_yuanbao_long_itn"
		"/apdcephfs_qy3/share_976139/data/asr/test/zh/test_20250228_yuanbao_pst1/pst_sf_20250202/text_itn"
	)	  
	curr_dir=`pwd`
	asr_engine=/apdcephfs_qy3/share_976139/programs/asr_bin
	for i in "${!names[@]}"; do
		dataset=${names[i]}
		ref=${paths[i]}
		hyp=$expdir/$decoding_name/context_test/$dataset/text.hyp
		cd $asr_engine
		bash run_post_adrenzhou.sh $curr_dir/$hyp
		cd $curr_dir
		python tools/compute-wer.py --char=1 --v=1 $ref $expdir/$decoding_name/context_test/$dataset/text_out.txt > $expdir/$decoding_name/context_test/$dataset/wer_itn
		res=`cat $expdir/$decoding_name/context_test/$dataset/wer_itn | grep "Overall ->"`
		echo $dataset, $res
	done	
fi

if [ $stage -le 3 ]; then
	# for 300 mode copy hyp
	echo "Computing Keywords Recall"
	if [ ! -f $expdir/$decoding_name/context_test/yuanbao_pst_test_long_300_kw_hunyuan ]; then
		cp -r $expdir/$decoding_name/context_test/yuanbao_pst_test_long_300_kw_wemeet $expdir/$decoding_name/context_test/yuanbao_pst_test_long_300_kw_hunyuan
	fi
# $orig_dir/context_test/yuanbao_pst_test_long_exact_kw_${version}.jsonl \
        for version in wemeet hunyuan; do
            for mode in 300 exact; do
                python scripts/compute-hw-recall.py \
				$orig_dir/context_test/depr/trans_${version}.txt \
				$expdir/$decoding_name/context_test/yuanbao_pst_test_long_${mode}_kw_${version}/text.hyp \
				> $expdir/$decoding_name/context_test/yuanbao_pst_test_long_${mode}_kw_${version}/kw_${version}.recall
				echo $expdir/$decoding_name/context_test/yuanbao_pst_test_long_${mode}_kw_${version}/kw_${version}.recall
				cat $expdir/$decoding_name/context_test/yuanbao_pst_test_long_${mode}_kw_${version}/kw_${version}.recall
	    	done            
			python scripts/compute-hw-recall.py \
			$orig_dir/context_test/depr/trans_${version}.txt \
			$expdir/$decoding_name/context_test/yuanbao_pst_test_long/text.hyp \
			> $expdir/$decoding_name/context_test/yuanbao_pst_test_long/kw_${version}.recall
			echo $expdir/$decoding_name/context_test/yuanbao_pst_test_long/kw_${version}.recall
			cat $expdir/$decoding_name/context_test/yuanbao_pst_test_long/kw_${version}.recall
        done
	# cat $expdir/$decoding_name/context_test/yuanbao_pst_test_long_*/*.recall
fi


if [ $stage -le 10 ]; then
	echo "Collecting Results"
        output_file=$expdir/$decoding_name/wer_results
		>$output_file
	find "$expdir/$decoding_name/" -name "wer*" | while read wer_file; do
	    # 提取文件夹名（wer文件所在目录的basename）
	    dir_name=$(basename $(dirname "$wer_file"))
	    wer=$(basename $wer_file)
	    # 从wer文件中提取"Overall ->"行（取第一行）
	    result_line=$(grep -m 1 "Overall ->" "$wer_file")
	    
	    # 如果找到了结果行，则写入输出文件
	    if [ -n "$result_line" ]; then
		echo "$dir_name $wer $result_line" >> "$output_file"
	    else
		echo "$dir_name No 'Overall ->' found" >> "$output_file"
	    fi
	done	
	cat $output_file
fi

exit 0
if [ $stage -le 1 ]; then

        echo "2. Evaluating DIAR"
        orig_dir=/apdcephfs_qy3/share_976139/users/adrenzhou/audio_llm/raw_data/processed
	for dataset in ${diar_list[@]}; do
		out_dir=$expdir/tests/$dataset
		mkdir -p $out_dir
		python batch_infer_qwen2_audio.py --input_data $orig_dir/${dataset}.jsonl --output_data $out_dir/result.jsonl --model_path $expdir --batch_size 3 --num_gpus $num_gpus

	done
fi

